<?php
namespace app\modules\merchant\controllers;

use app\common\controllers\AccessController;
use app\modules\merchant\forms\activity\ActivityForm;
use app\modules\merchant\services\ActivityService;
use app\utils\UserUtil;
use yii\base\UserException;

class ActivityController extends AccessController{

    //搜索
    public function actionSearch(){
        $merchant_id = UserUtil::getModuleUserIdentity("merchant")->getId();
        $pagesize = \Yii::$app->getRequest()->get("pagesize");

        return [
            'list'=>ActivityService::search($merchant_id,$page,$pagesize),
            'page'=>$page->Out(),
        ];
    }

    //添加
    public function actionAdd(){
        $form = new ActivityForm();
        $form->setScenario("create");
        if($form->load(\Yii::$app->getRequest()->post(),'') && ActivityService::create($form)){
            return "";
        }else{
            throw new UserException(ActivityService::getError());
        }
    }

    //编辑
    public function actionEdit(){
        $form = new ActivityForm();
        $form->setScenario("update");
        if($form->load(\Yii::$app->getRequest()->post(),'') && ActivityService::update($form)){
            return "";
        }else{
            throw new UserException(ActivityService::getError());
        }
    }

    //获取信息
    public function actionGetInfoById($id){
        return ActivityService::getInfoById(UserUtil::getModuleUserIdentity("merchant")->getId(),$id);
    }

    //删除
    public function actionDelete($id){
        ActivityService::delete(UserUtil::getModuleUserIdentity("merchant")->getId(),$id);
        return "";
    }
}